<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="clear" />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="edit(null)">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" :index="indexMthod" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态" width="80">
          <template #default="scope">
            {{ status(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="160">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text">
              <template>
                <el-button type="text" @click="open(scope.row.id)">重置密码</el-button>
              </template>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :page-sizes="[5, 8, 10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      title="添加员工"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="登录账号" prop="userName">
          <el-input v-model="form.userName" />

        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="form.phonenumber" />
        </el-form-item>
        <p>默认密码:123456</p>
        <el-form-item label="分配角色" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="员工状态" prop="status">
          <el-radio v-model="form.status" :label="0">禁用</el-radio>
          <el-radio v-model="form.status" :label="1">启用</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getStaffList, getAllRole, addStaff, editStaff, deleteStaff, resetPassword } from '@/api/employee'
export default {
  name: 'Employee',
  data() {
    return {
      params: {
        name: '',
        page: 1,
        pageSize: 5
      },
      employeeList: [],
      total: 0,
      form: {
        name: '',
        userName: '', // 登录账号,
        roleId: '', // 角色id
        status: null, // 状态
        phonenumber: '', // 手机号
        roleName: '123' // 角色名称
      },
      rules: {
        name: [
          { required: true, message: '请输入员工名字', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '手机号长度为11位', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      roleList: []
    }
  },
  created() {
    this.getemployeeList()
    this.getRoleList()
  },
  methods: {
    // 渲染列表
    async getemployeeList() {
      const res = await getStaffList(this.params)
      // console.log(res)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 搜索
    async search() {
      const res = await getStaffList(this.params)
      this.employeeList = res.data.rows
    },
    // 点击输入框 X
    clear() {
      console.log(1)
      this.params.page = 1
      this.getemployeeList()
    },
    // 当条数改变时
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.pageSize = val
      this.getemployeeList()
    },
    // 当页码改变时
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
      this.getemployeeList()
    },
    // 序号处理
    indexMthod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 状态处理
    status(status) {
      const obj = { 0: '未启用', 1: '启用', 3: '禁用' }
      return obj[status]
    },
    // 点击添加
    add() {
      // this.$refs.from.resetFields()
      this.dialogVisible = true
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getAllRole()
      // console.log(res)
      this.roleList = res.data
    },
    // 点击确定 提交表单
    confirmForm() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          await editStaff(this.form)
          this.$message.success('编辑成功')
        } else {
          await addStaff(this.form)
          this.$message.success('添加成功')
        }

        this.dialogVisible = false
        this.$refs.form.resetFields()
        this.getemployeeList()
      })
    },
    // 点击编辑 和添加
    edit(row) {
      this.dialogVisible = true
      this.form = {
        name: '',
        userName: '', // 登录账号,
        roleId: '', // 角色id
        status: null, // 状态
        phonenumber: '', // 手机号
        roleName: '123' // 角色名称
      }
      if (row !== null) {
        const { id, name, userName, roleId, status, phonenumber, roleName } = row
        this.form = { id, name, userName, roleId, status, phonenumber, roleName }
      }
      // console.log(row)
    },
    // 点击删除
    async del(id) {
      await deleteStaff(id)
      this.$message.success('删除成功')
      this.getemployeeList()
    },
    // 点击重置密码
    open(id) {
      const h = this.$createElement
      this.$msgbox({
        roundButtons: true,
        type: 'warning',
        message: h('p', null, [
          h('span', null, '您确定要重置密码为123456？ ')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '执行中...'
            setTimeout(() => {
              done()
              setTimeout(async() => {
                instance.confirmButtonLoading = false
                await resetPassword(id)
                this.$message.success('重置密码成功')
              }, 100)
            }, 1000)
          } else {
            done()
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
